/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.file;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import it.francescogabbrielli.android.solidalapp.Action;
import it.francescogabbrielli.android.solidalapp.Constants;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.EndpointsLoader;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.StatusEvent;
import it.francescogabbrielli.android.solidalapp.actions.GCMInsertResponseMessage;
import it.francescogabbrielli.android.solidalapp.deviceendpoint.model.Device;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;

public class ResponseFileActivity extends FileActivity<Request> {
	
	@Override
	public void onStatusUpdate(StatusEvent event) {
		
	}
	
	@Override
	public void onDataChanged(DataEvent event) {
		
		Device device = (Device) app.getData(DataKey.DEVICE);
		Response response = (Response) app.getData(DataKey.RESPONSE);
		
		switch(event.getKey()) {
		case DEVICE:
			if (device!=null && response==null)
				loadResponse(device.getId());
			break;
		case RESPONSE:
			if (response!=null)
				refreshFile();
			break;
		default:
		}
	}
	
	private void refreshFile() {
		Response response = (Response) app.getData(DataKey.RESPONSE);
		if (response!=null) {
			setTitle(getString(R.string.title_activity_file, response.getId()));
			reload(false, response.getRequestId(), Constants.LOADER_FILE_REQUEST);
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		if (app.getData(DataKey.FILE_REQUEST)==null)
			refreshFile();
		super.onNewIntent(intent);
	}
			
	@Override
	protected String getProfileFragmentName() {
		return ResponseFileFragment.class.getName();
	}
	
	@Override
	public DataKey getChatKey() {
		return DataKey.CHAT_RESPONSE;
	}
			
	@Override
	public void sendMessage(final long id, String message) {
		if (app.getData(DataKey.FILE_REQUEST_PROFILE)!=null) {
			Request req = (Request) app.getData(DataKey.FILE_REQUEST);
			Bundle data = EndpointsUtils.toBundle(req);
			data.putLong("id", id);
			data.putString("message", message);
			Action.start(this, GCMInsertResponseMessage.class, data);
		} else {
			execOnChatFragment(new ChatFragmentExec() {
				@Override
				public void exec(ChatFragment cf, boolean external) {
					cf.onMessageSent(id, getString(R.string.file_chat_not_connected));
				}
			});
		}
	}
	
	@Override
	public Loader<Request> onCreateLoader(int id, Bundle data) {
		return new EndpointsLoader<Request>(this, Request.class)
				.setFilters(data)
				.setMethod("getRequest");
	}

	@Override
	public void onLoadFinished(Loader<Request> loader, Request request) {
		app.setData(DataKey.FILE_REQUEST, request);
	}

	@Override
	public void onLoaderReset(Loader<Request> loader) {
	}
	
	private void loadResponse(long deviceId) {
		Bundle filters = new Bundle();
		filters.putLong("deviceId", deviceId);
		setSupportProgressBarIndeterminateVisibility(true);
		getSupportLoaderManager().initLoader(Constants.LOADER_RESPONSE, filters, new LoaderCallbacks<Response>() {
			@Override
			public Loader<Response> onCreateLoader(int id, Bundle data) {
				return new EndpointsLoader<Response>(ResponseFileActivity.this, Response.class, data)
						.setFilters(data)
						.setMethod("getLast");
			}
			@Override
			public void onLoadFinished(Loader<Response> loader, Response res) {
				setSupportProgressBarIndeterminateVisibility(false);
				if (res!=null)
					app.setData(DataKey.RESPONSE, res);
			}
			@Override
			public void onLoaderReset(Loader<Response> loader) {
			}
		});
		
	}
}
